package cn.school.service;

import cn.school.dao.ExamResultDao;
import cn.school.dao.StudentDao;
import cn.school.entity.Page;
import cn.school.entity.Student;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
@Service
//@Scope("prototype")
@Transactional(readOnly = true,timeout = 1000,propagation = Propagation.REQUIRED)
public class StudentService {
    @Autowired
    private StudentDao dao;
    @Autowired
    private ExamResultDao resultDao;
    public StudentService(){
        System.out.println("StudentService 被创建了！！！");
    }
    @Transactional(readOnly = false)
    public int add(Student stu) {
        return dao.add(stu);

    }

    //根据主键改其他列，一般只改一个实体
    @Transactional(readOnly = false)
    public int update(Student stu) {
        //加1分
        //加分操作
        int r= dao.update(stu);
        resultDao.incScore(stu.getStuno(), 2);
        return r;
    }

//    public int updatePass(@Param("stuno") long stuno, @Param("newPass") String newPass, @Param("oldPass") String oldPass);

    public int del(Student stu) {

        return dao.del(stu);

    }

    //根据主键，读取其他的信息
    public Student get(long stuno) {

        return dao.get(stuno);

    }


    /**
     * 查询条件，从第三个开始，如果没有，则忽略
     *
     * @param pageNo
     * @param pageSize
     * @param sname
     * @param qq
     * @param telephone
     * @return
     */
    public Page<Student> search(int pageNo, int pageSize, String sname, String qq, String telephone, Date start, Date end) {

        List<Student> list = dao.search(pageNo, pageSize, sname, qq, telephone, start, end);
        int total = dao.searchTotal(sname, qq, telephone, start, end);
        return new Page<Student>(pageNo, pageSize, total, list);

    }
}
